// 安装验收
<template>
    <f7-page class="common-page page-tab has-bottom-btns">
      <f7-navbar>
          <f7-nav-left>
              <f7-link back icon-material="chevron_left" force></f7-link>
          </f7-nav-left>
          <f7-nav-title>安装验收</f7-nav-title>
          <f7-nav-right></f7-nav-right>
      </f7-navbar>
      <div class="common-page-content">
        <van-tabs sticky>
          <van-tab name="#tab1" title="验收登记">
            <div id="tab1">
              <!-- 验收登记 -->
              <v-accordion title="验收资料" class="infoList">
                <div class="p15">
                  <v-picture-item
                    v-for="(item,index) in list$1"
                    :key="'list$1'+index"
                    :index="index"
                    :props="{ name: 'customName' }"
                    :item="item"
                    :dataKey="models.model.caseId"
                    :bizType='item.bizType'
                    listName="list$1"
                    ref="list$1"
                    :disabled="!canEdit"
                    :isDefaultFile="true"
                    :address="address"
                  />
                  <div class="df-box">
                    <div class="index">{{list$1.length+1}}</div>
                    <div class="right-content-box">
                      <div class="title">
                        <span style="color: #f15a5a;">*</span>
                        第三方检测报告
                      </div>
                      <van-field
                        readonly
                        clickable
                        placeholder="选择报告"
                        @click="canEdit?showReportList=true:''"
                        right-icon="arrow"
                        class="my-input"
                        :value="checkReport"
                      />
                      <qm-file-list
                        uploadId="qmfileList"
                        :isDefaultFile="true"
                        :defaultFiles="checkFileList"
                        :showEmpty="false"
                      ></qm-file-list>
                    </div>
                  </div>
                </div>
              </v-accordion>
              <v-accordion title="其他" class="infoList">
                <van-form input-align="right" ref="form">
                  <van-field
                    v-model="models.acceptance.acceptanceTime"
                    label="验收日期"
                    readonly
                    required
                    clickable
                    is-link
                    :rules="[{ required: true, message: '请填写验收日期' }]"
                    @click="canEdit?showCalendar = true:''"
                  />
                  <van-calendar v-model="showCalendar" :min-date="new Date(2018, 0, 1)" @confirm="onConfirm" color="#1990FF"/>
                  <van-field
                    v-model="models.model.memo"
                    label="补充说明"
                    type="textarea"
                    rows="5"
                    autosize
                    placeholder="补充说明"
                    :readonly="!canEdit"
                  />
                </van-form>
              </v-accordion>
            </div>
          </van-tab>
          <van-tab name="#tab2" title="流程进度">
            <!-- 流程进度 -->
            <div id="tab2" class="p15">
              <section class="qzjx-card">
                <div class="title" style="font-size:16px;">业务流程</div>
                <v-workflow-step :data="history"></v-workflow-step>
              </section>
              <section class="qzjx-card">
                <div class="title" style="font-size:16px;">操作记录</div>
                <v-workflow-list :data="controlData.workflow.history"></v-workflow-list>
              </section>
            </div>
          </van-tab>
        </van-tabs>

        <!-- 审核意见 -->
        <v-popup-opinion
            v-model="showPinion"
            :selectUser="selectUser"
            ref="opinion"
            @save="saveBack"
        >
        </v-popup-opinion>
        <!-- 审核意见 end -->
        <v-popup-opinion
            v-model="showPinion1"
            :selectUser="selectUser"
            ref="opinion"
            @save="savePass"
        >
        </v-popup-opinion>
        <v-popup-report-list
            v-model="showReportList"
            ref="opinion"
            @save="saveReport"
            url="/equipment/eqmCheck/queryList"
            v-if="controlData.property&&controlData.property.equipmentType"
            :equipmentType="controlData.property.equipmentType"
            :propertyId="controlData.property.propertyId"
            :isInstall="1"
        >
        </v-popup-report-list>
        <f7-toolbar position="bottom" no-shadow class="fff" v-if="(pageAction=='edit'&&taskCode=='usertask3')||canEdit">
          <f7-button fill class="submit-btn" @click="fnSubmit()"  v-if="canEdit">提交</f7-button>
          <f7-button fill class="submit-btn" @click="fnRollBackToProcess()" v-if="canRollBackToProcess">退回现场安装</f7-button>
          <!-- 施工审核/监理审核 -->
          <template v-if="pageAction=='edit'&&taskCode=='usertask3'">
            <f7-button fill class="submit-btn plain-btn" @click="fnCheck('showPinion')">退回</f7-button>
            <f7-button fill class="submit-btn" @click="fnCheck('showPinion1')">通过</f7-button>
          </template>
        </f7-toolbar>
      </div>
    </f7-page>
</template>

<script>
import controller from "@/libs/framework/workflowController"
import Config from "@/libs/config.js";
import vPopupReportList from '../components/v-popup-report-list.vue'
export default class instance extends controller {
    onBeforeInit () {
        this.urls = {
            model: ["/workflow/serviceGuideExt/getEntityByCode","/equipment/eqmInstallAcceptance/workflow/getWorkflowViewModel"],
        };
    }

    onInit () {
      this.$params.appCode = "installAcceptance";
    }
    onBeforeQuery (type, conditions, params) { }
    onAfterQuery (type, isSuccess, result) {
      if(type=='form'){
        if(result.controlData.progress){
          this.context.history=result.controlData.progress.data
          this.context.taskCode=result.controlData.progress.taskCode
        }else{
          this.context.history=[]
        }
      }else{
        if(!result.models.acceptance.acceptanceTime) {
          result.models.acceptance.acceptanceTime = this.vm.$formatter.formatDate(new Date(), 'yyyy-MM-dd')
        }else{
          result.models.acceptance.acceptanceTime = this.vm.$formatter.formatDate(result.models.acceptance.acceptanceTime, 'yyyy-MM-dd')
        }
        if(result.controlData.check&&result.controlData.check.checkId){
          this.context.checkFileList=result.controlData.check.checkFileList
          if(result.controlData.check.isQualified!=null||result.controlData.check.isQualified!=undefined){
            this.context.checkReport=`【${result.controlData.check.isQualified?'':'不'}合格】${result.controlData.check.checkCode?result.controlData.check.checkCode:'--'}`
          }
        }
        this.vm.getFileList('EQM_INSTALL_ACCEPTANCE',result.models.model.caseId,'list$1',result.controlData.property.equipmentType)
      }
      return result;
    }
    mixin () {
        return {
          components: {vPopupReportList},
            data () {
                return {
                  showReportList:false,
                  taskCode:"",
                  history:[],
                  models:{
                    acceptance:{},
                    model:{}
                  },
                  controlData:{
                    property:{},
                    workflow:{},
                    engineeBaseInfo:{},
                    maintenanceOrg:{},
                    installOrg:{},
                  },
                  showCalendar:false,
                  selectUser: {
                      selectComment: null,
                      comment: null,
                      comments: [],
                      tran: [],
                      activity: "",
                      users: [],
                  },
                  showPinion:false,
                  showPinion1:false,
                  list$1:[],
                  checkFileList:[],
                  checkReport:'',
                  address:'',
                };
            },
            computed: {
              canEdit(){
                return (this.pageAction=='create'||this.pageAction=='edit')&&this.taskCode=='usertask2'&&this.models.model.pstatus!=999?true:false
              },
              canRollBackToProcess(){
                return (this.pageAction=='create'||this.pageAction=='edit')&&this.taskCode=='usertask2'&&this.models.model.pstatus!=999?true:false
              }
            },
            created () {
              this.loadLocation()
            },
            methods: {
              //获取地址
              loadLocation() {
                  let self = this
                  self.$util.getCurrentPosition(
                      (pos) => {
                          if (pos.addresses) {
                              self.address = pos.addresses
                          }else if(pos.data) {
                              self.address = pos.data.name
                          }
                      },
                      (error) => {
                          // self.$f7.dialog.toast(
                          //     '获取定位失败,请确认是否授权定位功能'
                          // )
                      }
                  )
              },
              saveReport(val){
                this.checkFileList=[]
                this.checkFileList.push(...val.checkReportFileList)
                this.models.acceptance.checkId=val.checkId
                this.checkReport=`【${val.isQualified?'':'不'}合格】${val.checkCode||'--'}`
                this.$forceUpdate()
              },
              getFileList(typeCode,dataKey,name,attachCraneType){
                this.$f7.request.post(
                  '/admin/attachmentFormDetail/listUpload',
                  {typeCode,dataKey,attachCraneType},
                  (r) => {
                    r = JSON.parse(r);
                    if (r?.success&&r.parameters.fileList) {
                      this[name]=r.parameters.fileList
                      return
                    }
                    this[name]=[]
                    this.$framework.$f7.dialog.toast("获取文件失败");
                  }
                );
              },
              onConfirm(date) {
                this.models.acceptance.acceptanceTime = this.$formatter.formatDate(date, 'yyyy-MM-dd');
                this.showCalendar = false;
              },
              beforeCheck(){
                return new Promise((resolve, reject) => {
                  let num=0
                  this.list$1.forEach((item,index)=>{
                    console.log(this.$refs.list$1);
                    if(item.isMustUpload&&this.$refs.list$1[index].getUploadedCount()==0) num++
                  })
                  if(num>0){
                    resolve(false)
                  }else{
                    resolve(true)
                  }
                })
              },
              fnSubmit(){
                this.$refs.form.validate().then(async (_) => {
                  let flag=await this.beforeCheck()
                  if(!flag) return this.$toast('请上传必传附件')
                  if(!this.models.acceptance.checkId) return this.$toast('请选择第三方检测报告！')
                  this.showPinion1=true
                })
                .catch((err) => {
                  console.log(err);
                  this.$toast('请填写必填项！')
                });
              },
              fnRollBackToProcess() {
                  this.$f7.preloader.show();
                  this.$f7.request.postJSON(
                    "/equipment/eqmCase/rollBackToProcess",
                    {
                      caseId: this.models.model.caseId,
                    },
                    (r) => {
                      this.$f7.preloader.hide();
                      if (r && r.success) {
                        this.$f7.dialog.toast("退回成功！");
                        this.$f7router.back();
                      } else {
                        this.$f7.dialog.alert(r.message || "退回失败，请稍后再试");
                      }
                    }
                );
              },
              fnCheck(show){
                this[show]=true
              },
              saveBack(){
                this.super.fnBackJump(
                  this.selectUser.comment,
                  (result) => {
                    this.$f7.preloader.hide();
                    if (result.success) {
                      this.showPinion=false
                      this.refreshData('退回成功')
                    }else{
                      this.$f7.dialog.alert(result.message)
                    }
                  },
                  (e)=>{
                    console.log('退回失败---',e);
                    this.$f7.preloader.hide();
                  }
                )
              },
              refreshData(msg) {
                this.$f7.dialog.alert(msg, () => {
                  this.$f7.popup.close();
                  this.$f7.views.current.router.back({force:true})
                })
              },
              savePass(){
                this.showPinion1=false
                this.send();
              },
            },
            mounted () {},
        };
    }
}
</script>

<style lang="less" scoped>
@import url('../../../../assets/css/commonPage.less');
.common-page{
  /deep/ .accordion{
    background: #fff;
    margin-top: 10px!important;
  }
  /deep/ .van-field{
    input.van-field__control{
      font-size: 14px;
      height: auto;
    }
  }
  /deep/ .van-cell{
    padding: 16px;
  }
}

/deep/.my-input.van-field.van-cell{
  background-color: #F5F7FA;
  border: 1px solid #E4E8EB;
  border-radius: 4px;
  padding: 10px;
  margin: 10px 0 5px;
}
/deep/.van-field__body{
  textarea{
    font-size: 14px;
  }
}
</style>
